package com.binvshe.binvshe.account.regist.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.account.utils.MD5Util;
import com.binvshe.binvshe.account.utils.RegularUtils;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.frameworkbase.customviews.edittext.PasswordEditText;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegistPwdFragment extends BaseFragment {


    @Bind(R.id.pwdetPassword)
    PasswordEditText mPwdetPassword;
    @Bind(R.id.btnComplete)
    Button mBtnComplete;

    public static final String KEY_CAPTHCA = "KEY_CAPTCHA";

    private String mCaptcha;

    private String mPhone;

    public static RegistPwdFragment newInstance(String phone, String captcha) {

        Bundle args = new Bundle();
        RegistPwdFragment fragment = new RegistPwdFragment();
        args.putString(RegistCaptchaFragment.KEY_PHONE, phone);
        args.putString(KEY_CAPTHCA, captcha);
        fragment.setArguments(args);
        return fragment;
    }

    public RegistPwdFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mPhone = getArguments().getString(RegistCaptchaFragment.KEY_PHONE);
        mCaptcha = getArguments().getString(KEY_CAPTHCA);
        initToolBar();
        return view;
    }

    private void initToolBar() {
        getCustomToolbar().setVisibility(View.VISIBLE);
        getCustomToolbar().setTitle("设置密码");
        getCustomToolbar().enableBackButton().setOnClickListener(new BackBtnListener());
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_regist_pwd, null);
    }

    private String getPassWord() {
        return mPwdetPassword.getPassword().toString();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btnComplete)
    public void onClick() {
        if (TextUtils.isEmpty(getPassWord())) {
            FragmentUtils.showWarnDialog("密码不能为空", getFragmentManager());
            return;
        } else if (!RegularUtils.isPassword(getPassWord())) {
            FragmentUtils.showWarnDialog("密码长度为6-20位", getFragmentManager());
            return;
        }
        MyHttpRequest request = new MyHttpRequest(getContext());
        JSONObject jobj = new JSONObject();
        final String enCodePwd = MD5Util.getMd5(getPassWord());
        try {
            jobj.put(ParamKey.LOGIN_NAME, mPhone);
            jobj.put(ParamKey.PHONE, mPhone);
            jobj.put(ParamKey.PASSWORD, enCodePwd);
            jobj.put(ParamKey.CAPTCHA, mCaptcha);
//            jobj.put(ParamKey.TYPE, 1);
            request.post(UrlCenter.REGISTER, jobj, new DataRequestListener<Account>(Account.class) {

                @Override
                public void onSuccess(Account bean) {
                    Log.e("Login OnSuccess", " onSuccess  registe：r bean == " + bean);
                    EasyToast.showToast(getContext(), "注册成功!");
                    LoginActivity.start(getActivity());
                    getActivity().finish();
                }

                @Override
                public void onStart(Context context, String url) {
                    enableProgress(false);
                    super.onStart(context, url);
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    Log.e("Login onFailure", " onFailure register： str msg " + strMsg + " errorNo == " + errorNo);
                    EasyToast.showToast(getContext(), strMsg);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private class BackBtnListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            FragmentUtils.backStack(getActivity());
        }
    }
}
